package com.wuyb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuyb.enums.ErrorCode;
import com.wuyb.mapper.UserAdviceImgMapper;
import com.wuyb.mapper.UserAdviceMapper;
import com.wuyb.pojo.UserAdvice;
import com.wuyb.pojo.UserAdviceImg;
import com.wuyb.pojo.config.ImgPathPro;
import com.wuyb.pojo.entity.BaseResponse;
import com.wuyb.pojo.entity.ResultUtils;
import com.wuyb.service.UserAdviceService;
import com.wuyb.util.FileUtil;
import com.wuyb.util.MyUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: wyb
 * @Date: 2024/01/02/19:16
 * @Description:
 */
@Service
@Slf4j
public class UserAdviceServiceImpl extends ServiceImpl<UserAdviceMapper, UserAdvice> implements UserAdviceService {

    @Resource
    private UserAdviceMapper userAdviceMapper;

    @Resource
    private UserAdviceImgMapper userAdviceImgMapper;

    @Resource
    private ImgPathPro imgPathPro;

    @Resource
    private FileUtil fileUtil;

    /**
     * 获取通知列表
     * @param userAdvice
     * @return
     */
    private List<String> getAdviceImgs(UserAdvice userAdvice) {
        List<Object> objects = userAdviceImgMapper.selectObjs(new QueryWrapper<UserAdviceImg>()
                .eq("advice_id_xq", userAdvice.getId())
                .select("img_path_xq"));
        ArrayList<String> strings = new ArrayList<>();
        objects.forEach(x -> strings.add(x.toString()));
        return strings.size() > 0 ? strings : null;
    }

    /**
     * 分页返回通知列表
     * @param current
     * @param size
     * @return
     */
    @Override
    public BaseResponse<?> select(Integer current, Integer size) {
        IPage<UserAdvice> userAdviceIPage = userAdviceMapper.selectWithUserAndImg(new Page<>(current, size));
        userAdviceIPage.getRecords().forEach(x -> x.setImgs(getAdviceImgs(x)));
        return ResultUtils.success(userAdviceIPage);
    }

    /**
     * 新增反馈意见
     * @param userAdvice
     * @param files
     * @return
     */
    @Override
    public BaseResponse<?> insert(UserAdvice userAdvice, List<MultipartFile> files) {
        userAdvice.setUserId(MyUtil.getUser().getId());
        userAdvice.setTime(LocalDateTime.now());

        int result = userAdviceMapper.insert(userAdvice);

        for (MultipartFile file : files) {
            try {
                // 判断文件大小是否为0，如果是则表示该文件占用空文件，不能上传
                if (file.getSize() == 0) {
                    continue;
                }
                // 上传图片
                String filename = fileUtil.uploadImg(file, imgPathPro.getAdviceImg());
                assert filename != null;
                String filePath = Paths.get(imgPathPro.getAdviceImg()).resolve(filename).toString();

                userAdviceImgMapper.insert(new UserAdviceImg()
                        .setAdviceId(userAdvice.getId())
                        .setImgPath(filePath));

            } catch (Exception e) {
                log.error(e.getMessage());
                return ResultUtils.error(ErrorCode.OPERATION_ERROR.getCode(),"反馈失败".concat(e.getMessage()));
            }
        }
        if (result > 0) {
            return ResultUtils.success("感谢你的反馈");
        }

        return ResultUtils.error(ErrorCode.OPERATION_ERROR.getCode(),"反馈失败");
    }

    /**
     * 删除反馈意见
     * @param userId
     * @return
     */
    @Override
    public BaseResponse<?> delete(Integer userId) {

        int result = userAdviceMapper.deleteById(userId);
        if (result > 0) {
            return ResultUtils.success("感谢你的反馈");
        }
        return ResultUtils.error(ErrorCode.OPERATION_ERROR.getCode(),"反馈失败");
    }
}
